package com.example.guohuan.filelocker;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SettingActivity extends AppCompatActivity {

    MyDbHelper myDbHelper;
    SQLiteDatabase mDb;
    SQLiteDatabase mDb1;
    Cursor dbCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        myDbHelper = new MyDbHelper(SettingActivity.this);
        mDb = myDbHelper.getWritableDatabase();
        dbCursor = mDb.rawQuery("SELECT * FROM " + MyDbHelper.LOGIN_TABLE_NAME, null);

//        System.out.println("!!!!!dbCursor:"+dbCursor);

        final EditText oldPassword = (EditText) findViewById(R.id.oldPassword_editText);
        final EditText newPassword = (EditText) findViewById(R.id.newPassword_editText);
        Button setting = (Button) findViewById(R.id.setting_button);

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String p1 = oldPassword.getText().toString();
//                System.out.println("p1:"+p1);
                String p2 = newPassword.getText().toString();
//                System.out.println("p2:"+p2);

                String[] allColumns = new String[]{"_id", MyDbHelper.LOGIN_COL_PASSWORD};
                dbCursor = mDb.query(true,
                        MyDbHelper.LOGIN_TABLE_NAME, allColumns,
                        null, null, MyDbHelper.LOGIN_COL_PASSWORD, null, null,null);

                if (dbCursor != null)
                    dbCursor.moveToFirst();

                if (dbCursor.moveToFirst()){
                    String truePassword = dbCursor.getString(dbCursor.getColumnIndex(MyDbHelper.LOGIN_COL_PASSWORD));
//                    System.out.println("!!!!!!!truePassword:"+truePassword);
                    String trueId = dbCursor.getString(dbCursor.getColumnIndex("_id"));
//                    System.out.println("!!!!!!!id:"+trueId);



                    if(truePassword.matches(p1)){
                        ContentValues values = new ContentValues();
                        values.put(MyDbHelper.LOGIN_COL_PASSWORD,p2);
                        String whereClause = "_id=?";
                        String[] whereArgs = {trueId};
                        int result = mDb.update(MyDbHelper.LOGIN_TABLE_NAME,values,whereClause,whereArgs);
//                        System.out.println("!!!!!结果："+result);

//                        String[] allColumns1 = new String[]{MyDbHelper.LOGIN_COL_PASSWORD};
//                        dbCursor = mDb.query(true,
//                                MyDbHelper.LOGIN_TABLE_NAME, allColumns1, "_id=1",
//                                null, null, null, null,null);
//                        if (dbCursor != null)
//                            dbCursor.moveToFirst();
//
//                        if (dbCursor.moveToFirst()) {
//                            String updPassword = dbCursor.getString(0);
//                            System.out.println("!!!!!!!改过后的Password:" + updPassword);
//                        }




                        Toast.makeText(SettingActivity.this,"Setting successful",Toast.LENGTH_SHORT).show();
                        Intent newIntent = new Intent(SettingActivity.this,MainActivity.class);
                        startActivity(newIntent);
                    }
                    else{
                        Toast.makeText(SettingActivity.this,"Password incorrect",Toast.LENGTH_SHORT).show();
                    }

                }



            }
        });




    }
}
